using Digitalmes.Domain.Entities.Mes.Processes;

namespace Digitalmes.Application.Mes.Processes.StepMaterials.Queries;

/// <summary>
/// 工序物料查询命令。
/// </summary>
/// <param name="StepId"></param>
public sealed record GetStepMaterialListQuery(long StepId) : IRequest<IResult<List<ProcStepMaterialDto>>>;

internal sealed class GetStepMaterialListQueryHandler(IRepository<ProcStepMaterial> repo) : IRequestHandler<GetStepMaterialListQuery, IResult<List<ProcStepMaterialDto>>>
{
    public async Task<IResult<List<ProcStepMaterialDto>>> Handle(GetStepMaterialListQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .Includes(m => m.Material)
            .Where(m => m.StepId == request.StepId)
            .OrderBy(m => m.Seq)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<ProcStepMaterialDto>>());
    }
}
